package com.hospital.doctor.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hospital.common.constants.TokenConstants;
import com.hospital.common.domain.Admin;
import com.hospital.common.exceptions.ServiceException;
import com.hospital.common.result.R;
import com.hospital.common.utils.JwtUtils;
import com.hospital.common.utils.StringUtils;
import com.hospital.doctor.domain.ConsultRecordInfo;
import com.hospital.doctor.domain.DoctorEntity;
import com.hospital.doctor.service.EltDoctorEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author：zhuzhujiang
 * @Description:
 * @name：EltDoctorEntityController
 * @Date：2024/10/17 16:18
 */
@RestController
@RequestMapping("doctor")
public class EltDoctorEntityController {


    @Autowired
    private EltDoctorEntityService eltDoctorEntityService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;

    /**
     *医生修改问诊状态
     */
    @GetMapping("/updStatus/{status}")
    public R updStatus(@PathVariable("status") Integer status){

        if (status == null){
            throw new ServiceException("请输入问诊状态");
        }
        String token = request.getHeader("token");
        if (StringUtils.isNull(token)){
            throw new ServiceException("用户未登录", 401);
        }
        String userKey = JwtUtils.getUserKey(token);

        String user = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        Admin admin = JSON.parseObject(user, Admin.class);
        Long doctorId = admin.getId();

        boolean update = eltDoctorEntityService.update(new UpdateWrapper<DoctorEntity>()
                .lambda()
                .eq(DoctorEntity::getId, doctorId)
                .set(DoctorEntity::getStatus, status));
        if (!update){
            throw new ServiceException("修改失败");
        }

        return R.success();
    }

}
